<?php

/**
 * This class is for online shop order process.
 * params order step
 * This is the children of controller.
 * PHP5 CMS Typolight
 * Copyright is 34n.sam@gmail.com
 */
 
/**
 * Initialize the system
 */
define('TL_MODE', 'FE');
require('system/initialize.php');


class BackPocket extends Controller
{
	/**
	 * This is class construct function.
	 * @params null
	 * @Return null
	 */
	public function __construct()
	{
		parent::__construct();		
	}
	
	/**
	 * Page compile like the index page.
	 * @params null
	 * @Return null
	 */
	public function run()
	{	
		$arrProduct = array(
			'id' => $_POST['catalogid'],
			'size' => $_POST['size'],
			'color' => $_POST['color'],
			'quantity' => $_POST['quantity'],
			'is_wrapping' => intval($_POST['gift_wrapping']) ? $_POST['gift_wrapping'] : '0',
			'wrapping_price' => $_POST['gwPrice']
		);
		
		if (!isset($_SESSION['BACKPOCKET']) && !$_SESSION['BACKPOCKET'])
		{
			$_SESSION['BACKPOCKET'] = array();
			
			array_push($_SESSION['BACKPOCKET'], $arrProduct);
		}
		else
		{
			unset($_SESSION['BACKPOCKET']);
			$_SESSION['BACKPOCKET'] = array();
			
			array_push($_SESSION['BACKPOCKET'], $arrProduct);
		}
		
		if ($this->getLoginStatus('FE_USER_AUTH'))
		{
			$hash = sha1(session_id() . (!$GLOBALS['TL_CONFIG']['disableIpCheck'] ? $this->Environment->ip : '') . "FE_USER_AUTH");
			$time = time();
			$objSession = $this->Database->prepare("SELECT * FROM tl_session WHERE hash=? AND name=?")
										 ->limit(1)
										 ->execute($hash, 'FE_USER_AUTH');
										 
			$objBackpocket = $this->Database->prepare("SELECT * FROM tl_backpocket WHERE id_catalog=? AND id_member=? And size=? And color=?")
										 ->limit(1)
										 ->execute($_SESSION['BACKPOCKET'][0]['id'], $objSession->pid, $_SESSION['BACKPOCKET'][0]['size'], $_SESSION['BACKPOCKET'][0]['color']);			
										 
			if ($objBackpocket->numRows < 1)
			{			
				$objInsertId = $this->Database->prepare("INSERT INTO `tl_backpocket` (`id_member`, `id_catalog`, `tstamp`, `size`, `color`, `quantity`, `is_wrapping`, `wrapping_price`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
								->execute($objSession->pid, $_SESSION['BACKPOCKET'][0]['id'], $time, $_SESSION['BACKPOCKET'][0]['size'], $_SESSION['BACKPOCKET'][0]['color'], $_SESSION['BACKPOCKET'][0]['quantity'], $_SESSION['BACKPOCKET'][0]['is_wrapping'], $_SESSION['BACKPOCKET'][0]['wrapping_price']);
								
				unset($_SESSION['BACKPOCKET']);
			}
			else
			{
				$objResPocket = $this->Database->prepare("UPDATE `tl_backpocket` SET `tstamp`=?, `size`=?, `color`=?, `quantity`=?, `is_wrapping`=?, `wrapping_price`=? WHERE `id_backpocket`=?")
											->execute($time, $_SESSION['BACKPOCKET'][0]['size'], $_SESSION['BACKPOCKET'][0]['color'], $_SESSION['BACKPOCKET'][0]['quantity'], $_SESSION['BACKPOCKET'][0]['is_wrapping'], $_SESSION['BACKPOCKET'][0]['wrapping_price'], $objBackpocket->id_backpocket);
				
			}
			
			echo 'true';
		}
		else
		{
			echo 'false';
		}
	}
	
	/**
	 * Check whether a back end or front end user is logged in
	 * @param string
	 * @return boolean
	 */
	private function getLoginStatus($strCookie)
	{
		if (TL_MODE == 'FE' && $strCookie == 'BE_USER_AUTH' && !$this->Input->cookie('FE_PREVIEW'))
		{
			return false;
		}

		$hash = sha1(session_id() . (!$GLOBALS['TL_CONFIG']['disableIpCheck'] ? $this->Environment->ip : '') . $strCookie);

		if ($this->Input->cookie($strCookie) == $hash)
		{
			$this->import("Database");
			$objSession = $this->Database->prepare("SELECT * FROM tl_session WHERE hash=? AND name=?")
										 ->limit(1)
										 ->execute($hash, $strCookie);

			if ($objSession->numRows && $objSession->sessionID == session_id() && ($GLOBALS['TL_CONFIG']['disableIpCheck'] || $objSession->ip == $this->Environment->ip) && ($objSession->tstamp + $GLOBALS['TL_CONFIG']['sessionTimeout']) > time())
			{
				return true;
			}
		}

		return false;
	}
}

//$_SESSION['BACKPOCKET']
if (isset($_POST['catalogid']) && $_POST['catalogid'])
{
	$backpocket = new BackPocket();
	$backpocket->run();
}
?>